﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentUnitsAllocateService : BaseService, IEquipmentUnitsAllocateService
    {
        private readonly IEquipmentUnitsAllocateRepository _equipmentUnitsAllocateRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentUnitsAllocateService(IMapper mapper, IEquipmentUnitsAllocateRepository _equipmentUnitsAllocateRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._equipmentUnitsAllocateRepository = _equipmentUnitsAllocateRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentUnitsAllocate(EquipmentUnitsAllocate equipmentUnitsAllocate)
        {
            _equipmentUnitsAllocateRepository.AddEntity(equipmentUnitsAllocate);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentUnitsAllocate(long id)
        {
            var cust = _equipmentUnitsAllocateRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _equipmentUnitsAllocateRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<EquipmentUnitsAllocate> GetEquipmentUnitsAllocate(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _equipmentUnitsAllocateRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _equipmentUnitsAllocateRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentUnitsAllocate? GetEquipmentUnitsAllocateById(long id)
        {
            return _equipmentUnitsAllocateRepository.QueryEntityById(id);
        }

        public bool UpdEquipmentUnitsAllocate(EquipmentUnitsAllocate equipmentUnitsAllocate)
        {
            var aobj = _equipmentUnitsAllocateRepository.QueryIsEntityWhere(x => x.Id == equipmentUnitsAllocate.Id);
            if (aobj == null)
            {
                return false;
            }
            _equipmentUnitsAllocateRepository.UpdEntity(equipmentUnitsAllocate);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
